<template>
  <el-dialog
    title="查看用户详情"
    v-model="visible"
    top="10vh"
    width="70%"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      label-position="right"
      label-width="120px"
      :disabled="true"
      v-loading="loading"
    >
      <el-form-item label="登录帐号" prop="account">
        <el-input v-model="dataForm.account"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phoneNum">
        <el-input v-model="dataForm.phoneNum"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input v-model="dataForm.sex"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input type="textarea" v-model="dataForm.address"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button type="primary" @click="close">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, watch, type PropType } from 'vue'
import { UPDATE_MODEL_EVENT } from 'cm/contants'

import { validPhone } from '@/utils/validate'

import { getUserInfo, type ICreateOrUpdateUser } from '@/api/clientUser'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
    default: false
  },
  currId: {
    type: String,
    required: true,
    default: ''
  }
})

const emit = defineEmits([UPDATE_MODEL_EVENT, 'change'])

const visible = ref<boolean>(false)

const handleClose = () => {
  emit(UPDATE_MODEL_EVENT, false)
}

const defaultForm = {
  id: '',
  brandName: '',
  phone: '',
  wechat: '',
  content: ''
}
// 表单逻辑
const dataForm = ref<ICreateOrUpdateUser>({ ...defaultForm });

const loading = ref<boolean>(false)

// 查询用户详情
const getUserInfoApi = async (currId: string) => {
  loading.value = true
  const res = await getUserInfo(currId)
  loading.value = false
  if (res?.code === 200) {
    dataForm.value = res.data as ICreateOrUpdateUser;
  }
}

const close = () => {
  emit('change')
  handleClose()
}

watch(
  () => props.modelValue,
  async () => {
    visible.value = props.modelValue
    if (props.modelValue && props.currId) {
      await getUserInfoApi(props.currId)
    }
  }
)

</script>
